#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN DATA ABSTRACTION LAYER version 1

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using System.Data;
using Thn.Serialization;
#endregion

namespace Thn.Data.Mapping
{
    /// <summary>
    /// Provides a mapping between object attribute and data attribute
    /// </summary>
    public class AttributeMap
    {
        #region Serialize
        ISerializer mSerializer = null;

        #region On Get Serializer
        /// <summary>
        /// Get Serializer
        /// </summary>
        /// <returns></returns>
        protected virtual ISerializer OnGetSerializer()
        {
            XmlSerializer result = new XmlSerializer();
            result.RequiredAttributes = new Attribute[] { new SavableAttribute() };
            result.IncludeProperties = true;
            result.IncludeFields = true;

            return result;
        }
        #endregion

        #region Serialize
        /// <summary>
        /// Serialize value' type object to return value' type string
        /// </summary>
        protected virtual string Serialize(object value)
        {
            if (mSerializer == null) mSerializer = OnGetSerializer();
            return mSerializer.Serialize(value).AsString;
        }
        #endregion

        #region Deserialize
        /// <summary>
        /// Deserialize value' type string to return value' type object
        /// </summary>
        protected virtual object Deserialize(string value)
        {
            if (mSerializer == null) mSerializer = OnGetSerializer();
            return mSerializer.Deserialize(value);
        }
        #endregion
        #endregion

        #region Object Attribute
        private string mObjectAttribute = "";
        /// <summary>
        /// Gets/Sets name of object's attribute
        /// </summary>
        public string ObjectAttribute
        {
            get { return mObjectAttribute; }
            set { mObjectAttribute = value; }
        }
        #endregion

        #region Object Attribute Type
        private Type mObjectAttributeType;
        /// <summary>
        /// Gets/Sets .NET type of object's attribute
        /// </summary>
        public Type ObjectAttributeType
        {
            get { return mObjectAttributeType; }
            set { mObjectAttributeType = value; }
        }
        #endregion

        #region IsSpecialType
        private bool mIsSpecialType;
        /// <summary>
        /// Gets whether this map requires special attribute type handling
        /// </summary>
        public bool IsSpecialType
        {
            get { return mIsSpecialType; }
        }
        #endregion		

        #region IsArray
        private bool mIsArray;
        /// <summary>
        /// Gets/Sets whether object attribute is an array type
        /// </summary>
        public bool IsArray
        {
            get { return mIsArray; }
            set 
            {
                mIsArray = value;
                if (value) mIsSpecialType = true;
            }
        }
        #endregion

        #region IsEnum
        private bool mIsEnum;
        /// <summary>
        /// Gets/Sets whether object attribute is an enum type
        /// </summary>
        public bool IsEnum
        {
            get { return mIsEnum; }
            set 
            {
                mIsEnum = value;
                if (value) mIsSpecialType = true;
            }
        }
        #endregion

        #region IsList
        private bool mIsList;
        /// <summary>
        /// Gets/Sets whether object attribute is a list type
        /// </summary>
        public bool IsList
        {
            get { return mIsList; }
            set 
            {
                mIsList = value;
                if (value) mIsSpecialType = true;
            }
        }
        #endregion		

        #region IsIdentifier
        private bool mIsIdentifier;
        /// <summary>
        /// Gets/Sets whether object attribute is an indentifier of object
        /// </summary>
        public bool IsIdentifier
        {
            get { return mIsIdentifier; }
            set { mIsIdentifier = value; }
        }
        #endregion

        #region IsAutoGenerated
        private bool mIsAutoGenerated = false;
        /// <summary>
        /// Gets/Sets whether object attribute is auto-generated by the underlying database engine
        /// </summary>
        public bool IsAutoGenerated
        {
            get { return mIsAutoGenerated; }
            set { mIsAutoGenerated = value; }
        }
        #endregion		

        #region Data Attribute
        private string mDataAttribute = "";
        /// <summary>
        /// Gets/Sets the corresponding data attribute (usually the equivalent column)
        /// </summary>
        public string DataAttribute
        {
            get { return mDataAttribute; }
            set { mDataAttribute = value; }
        }
        #endregion
        
        #region Required
        private bool mRequired;
        /// <summary>
        /// Gets/Sets whether this attribute can be null or empty
        /// </summary>
        public bool IsRequired
        {
            get { return mRequired; }
            set { mRequired = value; }
        }
        #endregion            

        #region Size
        private int mSize;
        /// <summary>
        /// Gets/Sets maximum length of this attribute
        /// </summary>
        public int Size
        {
            get { return mSize; }
            set { mSize = value; }
        }
        #endregion

        #region Data Type Specified
        private bool mDataAttributeTypeSpecified;
        /// <summary>
        /// Gets whether this map needs a user-specified DbType or just let the dialect handles it
        /// </summary>
        public bool DataAttributeTypeSpecified
        {
            get { return mDataAttributeTypeSpecified; }            
        }
        #endregion
		
        #region Type
        private object mDataAttributeType;
        /// <summary>
        /// Gets/Sets DbType. If not set, infer from dialect
        /// </summary>
        public object DataAttributeType
        {
            get { return mDataAttributeType; }
            set 
            {
                mDataAttributeType = value;
                mDataAttributeTypeSpecified = true;
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public AttributeMap()
        {
        }

        /// <summary>
        /// Full constructor
        /// </summary>
        public AttributeMap(string objectAttribute, string dataAttribute)
        {
            mObjectAttribute = objectAttribute;
            mDataAttribute = dataAttribute;
        }
        #endregion
    }
}
